//
//  DiscoverViewModel.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import Foundation
import Combine

class DiscoverViewModel: ObservableObject {
    @Published var moments: [Moment] = []
    @Published var isLoading: Bool = false
    @Published var showNewMomentSheet: Bool = false
    @Published var newMomentText: String = ""
    @Published var selectedImages: [String] = []
    
    private let mockDataService = MockDataService.shared
    
    init() {
        loadMoments()
    }
    
    func loadMoments() {
        isLoading = true
        
        // 模拟网络延迟
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.moments = self.mockDataService.getMockMoments()
            self.isLoading = false
        }
    }
    
    func refreshMoments() {
        loadMoments()
    }
    
    func toggleLike(for moment: Moment) {
        if let index = moments.firstIndex(where: { $0.id == moment.id }) {
            if moments[index].isLikedByCurrentUser {
                moments[index].likes.removeAll { $0.userName == User.currentUser.userName }
            } else {
                moments[index].likes.append(User.currentUser)
            }
        }
    }
    
    func addComment(to moment: Moment, content: String) {
        if let index = moments.firstIndex(where: { $0.id == moment.id }) {
            let comment = MomentComment(
                user: User.currentUser,
                content: content,
                timestamp: Date()
            )
            moments[index].comments.append(comment)
        }
    }
    
    func publishMoment() {
        guard !newMomentText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
        
        let contentType: MomentContentType
        if selectedImages.isEmpty {
            contentType = .text
        } else if selectedImages.count == 1 {
            contentType = .singleImage
        } else {
            contentType = .multipleImages
        }
        
        let newMoment = Moment(
            user: User.currentUser,
            content: newMomentText,
            contentType: contentType,
            images: selectedImages
        )
        
        moments.insert(newMoment, at: 0)
        
        // 重置发布状态
        newMomentText = ""
        selectedImages = []
        showNewMomentSheet = false
    }
    
    func addImage(_ imageName: String) {
        if selectedImages.count < 9 {
            selectedImages.append(imageName)
        }
    }
    
    func removeImage(at index: Int) {
        if index < selectedImages.count {
            selectedImages.remove(at: index)
        }
    }
    
    func formatTimeAgo(_ date: Date) -> String {
        let now = Date()
        let timeInterval = now.timeIntervalSince(date)
        
        if timeInterval < 60 {
            return "刚刚"
        } else if timeInterval < 3600 {
            let minutes = Int(timeInterval / 60)
            return "\(minutes)分钟前"
        } else if timeInterval < 86400 {
            let hours = Int(timeInterval / 3600)
            return "\(hours)小时前"
        } else {
            let days = Int(timeInterval / 86400)
            if days == 1 {
                return "昨天"
            } else if days < 7 {
                return "\(days)天前"
            } else {
                let formatter = DateFormatter()
                formatter.dateFormat = "MM月dd日"
                return formatter.string(from: date)
            }
        }
    }
}
